package com.dys.hejiayuncommunity.web.controller.system;

import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.core.page.PageResult;
import com.dys.hejiayuncommunity.common.utils.SecurityUtils;
import com.dys.hejiayuncommunity.system.domain.SysDictData;
import com.dys.hejiayuncommunity.system.service.SysDictDataService;
import com.dys.hejiayuncommunity.system.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: SysDictDataController
 * @Description: 数据字典信息
 * @CreateDate: 2024/12/26
 * @Version: v1.0.0
 */
@RestController
@RequestMapping(value = {"/system/dict/data"})
public class SysDictDataController extends BaseController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 查询字典数据列表
     *
     * @param sysDictData 字典数据
     * @return 字典数据列表
     */
    @GetMapping(value = {"/list"})
    public PageResult list(SysDictData sysDictData) {
        super.startPage();
        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(sysDictData);
        return super.getData(dictDataList);
    }

    /**
     * 根据ID查询字典详情信息
     *
     * @param dictCode 字典编码
     * @return 字典详细信息
     */
    @GetMapping(value = {"/{dictCode}"})
    public BaseResponse getInfo(@PathVariable Long dictCode) {
        return BaseResponse.success(sysDictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 字典数据
     */
    @GetMapping(value = {"/type/{dictType}"})
    public BaseResponse getDictByType(@PathVariable String dictType) {
        return BaseResponse.success(sysDictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典数据
     *
     * @param sysDictData 字典数据
     * @return 受影响行数
     */
    @PostMapping
    public BaseResponse add(@RequestBody SysDictData sysDictData) {
        sysDictData.setCreateBy(SecurityUtils.getUserName());
        return super.toAjax(sysDictDataService.insertDictData(sysDictData));
    }

    /**
     * 修改字典类型
     *
     * @param sysDictData 字典数据
     * @return 受影响行数
     */
    @PutMapping
    public BaseResponse edit(@RequestBody SysDictData sysDictData) {
        sysDictData.setUpdateBy(SecurityUtils.getUserName());
        return super.toAjax(sysDictDataService.updateDictData(sysDictData));
    }

    /**
     * 删除字典类型
     *
     * @param dictCodes 字典编码
     * @return 受影响行数
     */
    @DeleteMapping(value = {"/{dictCodes}"})
    public BaseResponse remove(@PathVariable Long[] dictCodes) {
        return super.toAjax(sysDictDataService.deleteDictDataByIds(dictCodes));
    }
}
